package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupListParam;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Repository
public class UserGroupRepositoryImpl implements UserGroupRepository {
    @Autowired
    private UserGroupMapper userGroupMapper;

    public List<UserGroupVO> listUserGroups(Integer businessId){
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        List<UserGroupPO> defaultList = userGroupMapper.selectList(queryWrapper);
        List<UserGroupVO> resultList = new ArrayList<>();
        defaultList.forEach(po -> {
            UserGroupVO vo = new UserGroupVO();
            BeanUtils.copyProperties(po,vo);
            resultList.add(vo);
        });
        return resultList;
    }
    public void save(UserGroupPO po){
        userGroupMapper.insert(po);
    }

    @Override
    public PageResult<UserGroupVO> pageList(UserGroupListParam userGroupListParam) {
//        PageHelper.startPage(userGroupListParam.getPageNum(), userGroupListParam.getPageSize());
//        List<UserGroupPO> userGroupPOS = userGroupMapper.selectList(null);
        Page<UserGroupPO> page = new Page<>(userGroupListParam.getPageNum(), userGroupListParam.getPageSize());
        Page<UserGroupPO> userGroupPOPage = userGroupMapper.selectPage(page, null);

        List<UserGroupVO> userGroupVOS = userGroupPOPage.getRecords().stream().map(po -> {
            UserGroupVO userGroupVO = new UserGroupVO();
            BeanUtils.copyProperties(po, userGroupVO);
            return userGroupVO;
        }).toList();

        PageResult<UserGroupVO> pageResult = new PageResult<>();
        pageResult.setTotal(userGroupPOPage.getTotal());
        pageResult.setPageNo((int) userGroupPOPage.getCurrent());
        pageResult.setPageSize((int) userGroupPOPage.getSize());
        pageResult.setObjects(userGroupVOS);

        return pageResult;
    }

    @Override
    public void save(UserGroupParam param) {
        UserGroupPO userGroupPO = new UserGroupPO();
        BeanUtils.copyProperties(param, userGroupPO);
        userGroupPO.setCreateAt(new Date());
        userGroupMapper.insertOrUpdate(userGroupPO);
    }

    @Override
    public void del(Integer id) {
        userGroupMapper.deleteById(id);
    }

    @Override
    public Result<UserGroupVO> detail(Integer id) {
        UserGroupPO userGroupPO = userGroupMapper.selectById(id);
        UserGroupVO userGroupVO = new UserGroupVO();
        BeanUtils.copyProperties(userGroupPO, userGroupVO);

        return new Result<>(userGroupVO);
    }
}
